{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![image](https://jupyterlite.rtfd.io/en/latest/_static/badge.svg)](https://demo.leafmap.org/lab/index.html?path=maplibre/duckdb_layer.ipynb)\n",
    "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/leafmap/blob/master/docs/maplibre/duckdb_layer.ipynb)\n",
    "[![image](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/opengeos/leafmap/HEAD)\n",
    "\n",
    "**Add DuckDB Vector Tiles**\n",
    "\n",
    "Uncomment the following line to install [leafmap](https://leafmap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install \"leafmap[duckdb]\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import leafmap.maplibregl as leafmap"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you are running in a remote Jupyter environment, you need to configure leafmap with your JupyterHub URL. Uncomment the following line and replace `https://your-jupyterhub-domain.com` with your JupyterHub URL."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# leafmap.configure_jupyterhub(\"https://your-jupyterhub-domain.com\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Download a sample GeoParquet dataset from [Source Coop](https://source.coop/giswqs/nwi/wetlands/RI_Wetlands.parquet)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://data.source.coop/giswqs/nwi/wetlands/RI_Wetlands.parquet\"\n",
    "fileapth = leafmap.download_file(url)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example 1: Load data from vector file(creates temporary database)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m = leafmap.Map(style=\"liberty\", height=\"600px\")\n",
    "m.add_basemap(\"Esri.WorldImagery\")\n",
    "m.add_duckdb_layer(\n",
    "    data=fileapth,  # Supports GeoParquet, GeoJSON, and GeoPackage, etc.\n",
    "    layer_name=\"wetlands\",\n",
    "    layer_type=\"fill\",\n",
    "    paint={\"fill-color\": \"#3388ff\"},\n",
    "    opacity=0.5,\n",
    "    fit_bounds=True,\n",
    "    use_view=False,  # For remote datasets, set use_view to True to avoid copying data to local\n",
    "    min_zoom=None,  # For large datasets, set min_zoom to None to avoid zooming out too much\n",
    "    quiet=False,\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m.close_db_connections()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Example 2: Load from existing DuckDB database (no data loading)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://data.gishub.org/duckdb/nyc_data.db.zip\"\n",
    "leafmap.download_file(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "db_path = \"nyc_data.db\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#\n",
    "m = leafmap.Map(\n",
    "    center=[-73.9031255, 40.7127753], zoom=9, style=\"liberty\", height=\"600px\"\n",
    ")\n",
    "m.add_duckdb_layer(\n",
    "    database_path=db_path,\n",
    "    table_name=\"nyc_neighborhoods\",\n",
    "    layer_type=\"fill\",\n",
    "    paint={\"fill-color\": \"#ff0000\"},\n",
    "    opacity=0.5,\n",
    "    fit_bounds=False,\n",
    "    src_crs=\"EPSG:26918\",\n",
    "    quiet=True,\n",
    ")\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m.close_db_connections()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "geo",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
